<?php
    require_once('tablas.inc.php');
    require_once('db.php');

    try {
      $layer = $_POST['activeLayer'];
      $layerTitle = $_POST['activeLayerTitle'];
      $geometry = $_POST['geometry'];
      $queryType = $_POST['ctrlRadio'];
    }
    catch( Exception $e ) {
      die('Error: Faltan datos para la consulta.');
    }

    // Calcula la tolerancia de acuerdo al nivel de zoom. 
    // $zoomLevel = 0 -> tolerancia = 0.5
    // $zoomLevel = 8 -> tolerancia = 0.01
    function getTolerance( $zoomLevel ) {
      return -0.06125*$zoomLevel + 0.5;
    }

    $buffer = "";

    if( $queryType == 'qPoint' )
    {
      $zoom = getTolerance( $_POST['zoomLevel'] );
      $buffer = "ST_Buffer( 'SRID=4326;$geometry', $zoom )";
    }
    elseif( $queryType == 'qPolygon' )
    {
      $buffer = "'$geometry'";
    }
    else
      die( 'Error: tipo de consulta desconocido' );

    $queryString = "SELECT " . join(',', $tablas[$layer]) . " FROM $layer WHERE ST_Intersects( " .
                          "ST_AsText( the_geom )," . 
                          "ST_AsText( $buffer )" .
                     ")";

    try {
      $query = $db->prepare( $queryString );
      $query->execute();
    }
    catch ( PDOException $e ) {
      die( 'Error: ' . $e->getMessage() );
    }

    $rowNumber = 0;
?>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Resultado de consulta de geometrías</title>
    <link rel="shortcut icon" href="favicon.png">
    <link rel="stylesheet" type="text/css" href="estilos.css">
    <script src="jquery-1.7.min.js" type="text/javascript"></script>
    <script type="text/javascript"> $(document).ready( function() { $('#buttonDiv button').click( function() { window.close() } ) } ); </script>
  </head>
  <body>
    <div class="resultadoTitle"><h1>Resultados</h1></div>
    <div class="layerName">Capa: <span class="resultLayerTitle"><?php echo $layerTitle ?></span>
      <div class="resultCount"><span class="resultado3"><?php echo $query->rowCount() ?></span> resultados</div>
    </div>
    <table border="1" id="tableResults">
      <thead>
        <tr>
          <?php foreach( $tablas[$layer] as $field ): ?>
            <th><?php echo $fieldTitles[ $field ]; ?></th>
          <?php endforeach; ?>
        </tr>
      </thead>
      <tbody>
        <?php while( $row = $query->fetch( PDO::FETCH_ASSOC ) ): ?>
          <?php if( $rowNumber++ % 2 == 0 ): ?>
            <tr>
          <?php else: ?>
            <tr class="alt">
          <?php endif; ?>
            <?php foreach( $row as $value ): ?>
              <td><?php echo $value; ?></td>
            <?php endforeach; ?>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
    <div class="layerName">Capa: <span class="resultLayerTitle"><?php echo $layerTitle ?></span>
      <div class="resultCount"><span class="resultado3"><?php echo $query->rowCount() ?></span> resultados</div>
    </div>
    <div id="buttonDiv"><button class="styledButton">Cerrar</button></div>
  </body>
</html>